package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.teacher.pojo.entity.Behavior;
import com.teacher.pojo.entity.StudyGrades;
import com.teacher.pojo.excel.BehaviorExcel;
import com.teacher.pojo.excel.StudyGradesExcel;
import com.teacher.service.IBehaviorService;
import com.teacher.service.IPerStudentService;
import com.teacher.service.IStudyGradesService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelStudyGradesListener extends AnalysisEventListener<StudyGradesExcel> {
    private Long termId;
    private IStudyGradesService iStudyGradesService;

    private List<StudyGrades> studyGradesList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelStudyGradesListener(Long termId, IStudyGradesService iStudyGradesService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iStudyGradesService = iStudyGradesService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(StudyGradesExcel studyGradesExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(studyGradesExcel.getStudentKey())) {
            StudyGrades studyGrades = new StudyGrades();
            BeanUtils.copyProperties(studyGradesExcel, studyGrades);
            studyGrades.setTermId(termId);
            studyGrades.setHasNotPass(studyGradesExcel.getHasNotPass().equals("是"));
            studyGradesList.add(studyGrades);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iStudyGradesService.saveBatch(studyGradesList);
    }
}
